<?php

// [ 位移 ]
// use app\common\lib\Loc;
// $no   = '662862';
// $item = [2,4,10,7,8,9,1,5,3,6];
// Loc::run($no,$item);

namespace app\common\lib;
use think\Db;

class Loc
{
    public static function run($no, $results)
    {
        $res = Db::name('pk10')->where('no',--$no)->field('no,create_time',true)->find();
        // p($res);
        if ( is_null($res) ) return false;

        $item['v1'] = $results[0];
        $item['v2'] = $results[1];
        $item['v3'] = $results[2];
        $item['v4'] = $results[3];
        $item['v5'] = $results[4];
        // p($item);

        $data = [];
        $data['no'] = ++$no;
        foreach ($item as $key => $val) $data[$key . '_loc'] = self::_fdLoc($val,$res);
        // p($data);die;

        $ret = Db::name('loc')->where('no',$data['no'])->find();
        // p($ret);die;
        if ( !is_null($ret) ) return true;

        $num = Db::name('loc')->insert($data);
        // p($num);die;

        return ($num > 0);
    }

    // 定位
    private static function _fdLoc($val, $res)
    {
        $loc = 0;
        foreach ($res as $k => $v) {
            if ( $val == intval($v) ) {
                $loc = ltrim($k,'v');
                break;
            }
        }
        // p($loc);die;
        return $loc;
    }
}